﻿using MPic.Business.DAL;
using MPic.Business.Entity;
using MPic.Business.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MPic.Business.BLL
{
    public class UserAlbumBLL
    {
        private UserAlbumDAL userAlbumDal = new UserAlbumDAL(DbConnectionEnum.MPic);
        private static object locker = new object();
        private static UserAlbumBLL _instance;
        public static UserAlbumBLL Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (locker)
                    {
                        if (_instance == null)
                        {
                            _instance = new UserAlbumBLL();
                        }
                    }
                }
                return _instance;
            }
        }

        /// <summary>
        /// 增加相册
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="albumName"></param>
        public int Insert(int userId, string albumName, bool isDefault)
        {
            var userAlbum = new UserAlbumEntity
            {
                AlbumName = albumName.Trim(),
                OwnerId = userId,
                FilesNumber = 0,
                CreateTime = DateTime.Now,
                ModifyTime = DateTime.Now,
                SortFlag = DateTime.Now,
                IsDefault = isDefault
            };
            return userAlbumDal.Insert(userAlbum).Value;
        }

        /// <summary>
        /// 获取指定用户相册
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public List<UserAlbumEntity> Get(int userId)
        {
            return userAlbumDal.GetList(new { OwnerId = userId }, "SortFlag DESC").ToList();
        }

        /// <summary>
        /// 更新相册相片数量
        /// </summary>
        /// <param name="albumId"></param>
        public void Update(int albumId)
        {
            var album = userAlbumDal.Get(albumId);
            album.FilesNumber += 1;
            userAlbumDal.Update(album);
        }
    }
}
